﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using System.Web.Script.Serialization;
using FubiManger.Controls;
using Smobiler.Device;

namespace FubiManger.Pad
{
    ////ToolboxItem用于控制是否添加自定义控件到工具箱，true添加，false不添加
    //[System.ComponentModel.ToolboxItem(true)]
    partial class Ordrightcontrol : Smobiler.Core.Controls.MobileUserControl
    {
        public Ordrightcontrol() : base()
        {
            //This call is required by the SmobilerUserControl.
            InitializeComponent();
        }
        /// <summary>
        /// 订单号
        /// </summary>
        public string ordsn { get; set; }
        private ms_order order = new ms_order();
        List<Gdpro> prolist = new List<Gdpro>();
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        private ms_merchant merchant = new ms_merchant();
        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnclare_Press(object sender, EventArgs e)
        {
            Pmessagecontrol con = new Pmessagecontrol();
            con.title = "订单提示";
            con.des = "是否确认取消订单，取消订单将不可恢复";
            this.Form.ShowDialog(con, (obj, args) =>
            {
                if (con.flag == true)
                {
                    if (orderDal.Claerordsn(ordsn) > 0)
                    {
                        order_markDal.Delbyordsn(ordsn);
                        ((PmainForm)this.Form).Closeinfo("1");
                    }
                }
            });
        }

        private void Ordrightcontrol_Load(object sender, EventArgs e)
        {
            merchant = merchantDal.Querybyid(merchantid);
            Getorder();
        }
        /// <summary>
        /// 获取订单详情
        /// </summary>
        private void Getorder()
        {
            order = orderDal.Querybysn(ordsn);
            if (order.user_tel.Length == 11)
            {
                lbltel.Text = order.user_tel.Substring(0, 3) + "****" + order.user_tel.Substring(7, 4);
                btndist.Enabled = false;
            }
            else
            {
                lbltel.Text = "现场下单";
            }
            if (!string.IsNullOrEmpty(order.orderdisksn))
            {
                lbldisksn.Text = "餐桌号：" + order.orderdisksn;//餐桌号
                lbldisksn.Visible = true;
            }
            lblordsn.Text = order.order_sn;//订单编号
            lbltime.Text = Convert.ToDateTime(order.add_time).ToString("yyyy-MM-dd HH:mm:ss");
            lblprice.Text = "￥" + order.pay_price;//实付金额           
            if (order.orderremark.Length > 3)
                lblremark.Text = order.orderremark;//备注
            else
            {
                pantp.Height = 98;
                lblremark.Visible = false;

            }
            JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
            prolist = js.Deserialize<List<Gdpro>>(order.goods_info);
            panpros.Height = prolist.Count * 44;
            panord.Height = panpros.Height + 30;
            panbtm.Location = new System.Drawing.Point(0, panord.Height + 100);
            pdesc.Height = pantp.Height + panord.Height + panbtm.Height + 8;
            panmiddle.Height = pdesc.Height + 25;
            //panall.Height = panord.Height + pantp.Height + 40;
            for (int i = 0; i < prolist.Count; i++)
            {
                ordergpcontrol con = new ordergpcontrol();
                con.proname = prolist[i].proname;
                con.cout = "x " + prolist[i].count;
                con.price = "￥ " + prolist[i].price;
                panpros.Controls.Add(con);
            }
        }

        private void btnclose_Press(object sender, EventArgs e)
        {
            // ((PmainForm)this.Form).Closeinfo("2");
            this.Close();
        }
        /// <summary>
        /// 结算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnsettlement_Press(object sender, EventArgs e)
        {
            if (order_markDal.Addrecord(order.order_sn, order.goods_info, order.merchant_id, order.pay_price) > 0)
            {
                btnsettlement.Enabled = false;
                button1.Enabled = false;
                btndist.Enabled = false;
            }
        }
        /// <summary>
        /// 已收款
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnend_Press(object sender, EventArgs e)
        {
            Pmessagecontrol con = new Pmessagecontrol();
            con.title = "收款提示";
            con.des = "是否确定已收款";
            this.Form.ShowDialog(con, (obj, args) =>
            {
                if (con.flag == true)
                {
                    orderDal.Updatecodestatus(ordsn);//改变订单状态
                    order_markDal.Delbyordsn(ordsn);//删除副屏显示记录
                    btnend.Enabled = false;
                    btnclare.Enabled = false;
                    button1.Enabled = false;
                    btndist.Enabled = false;
                    btnsettlement.Enabled = false;
                }
            });

        }
        /// <summary>
        /// 抹零
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Press(object sender, EventArgs e)
        {
            double paymoney = Convert.ToDouble(order.pay_price);
            int money = (int)paymoney;
            order.pay_price = money.ToString();
            orderDal.Updateprice(ordsn, paymoney.ToString());
            lblprice.Text = "￥" + order.pay_price;//实付金额

        }
        /// <summary>
        /// 会员折扣
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btndist_Press(object sender, EventArgs e)
        {

            Pdiscountcontrol con = new Pdiscountcontrol();
            con.merchantid = merchantid;//商户ID
            con.ordsn = ordsn;//订单号
            this.Form.ShowDialog(con, (obj, args) =>
            {
                if (!string.IsNullOrEmpty(con.usertel))
                {
                    if (con.usertel.Length == 11)
                    {
                        lbltel.Text = con.usertel.Substring(0, 3) + "****" + order.user_tel.Substring(7, 4);
                    }
                }
                if (con.discount_rate > 0)
                {
                    double price = Convert.ToDouble(order.pay_price) * con.discount_rate;
                    // lbldiscount.Text = "优惠:￥" + (Convert.ToDouble(order.pay_price) - price).ToString();
                    orderDal.Updateprice(ordsn, price.ToString());
                    lblprice.Text = "金额：￥" + price.ToString();
                }

            });
        }
        /// <summary>
        /// 加单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnadd_Press(object sender, EventArgs e)
        {
            porderfoodcontrol con = new porderfoodcontrol();
            con.merchantid = merchantid;//商户ID
            con.ordersn = ordsn;//订单编号
            con.carlist = prolist;//购物车商品
            this.Form.ShowDialog(con, (obj, args) =>
            {
                panpros.Controls.Clear();
                Getorder();//获取订单详情
            });
        }
        /// <summary>
        /// 打印小票
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnprint_Press(object sender, EventArgs e)
        {
            int count = 0;
            string printtxt = "TEXT 0,0,\"TSS16.BF2\",0,1,2,\"" + merchant.merchant_name + "\"\r\n";
            string str2 = "位置："+order.orderdisksn;
            string str3 = "----------------------------------";
            string str4 = "下单时间：" + order.add_time;
            string str5 = "订单编号：" +order.order_sn;
            string str6 = "下单人：" + this.Form.Session["nickname"].ToString();
            string str7 = "----------------------------------";
            string str10 = "----------------------------------";
            string str11 = "合计："+order.pay_price;
            string str12 = "应付："+order.pay_price;
            string str13 = "使用数字人民币，就上福赑小店";
            List<string> strlist = new List<string>();
            strlist.Add(printtxt);
            strlist.Add(str2);
            strlist.Add(str3);
            strlist.Add(str4);
            strlist.Add(str5);
            strlist.Add(str6);
            strlist.Add(str7);
            
           
            for (int i = 0; i < prolist.Count; i++)
            {
                string pstr = prolist[i].proname;
                string pstr1= "                 x" + prolist[i].count  + "     ￥" + prolist[i].price;
                strlist.Add(pstr);
                strlist.Add(pstr1);
            }
           
            strlist.Add(str10);
            strlist.Add(str11);
            strlist.Add(str12);
            strlist.Add(str13);
            for (int i = 0; i < strlist.Count; i++)
            {
                count = (i + 1) * 35;
                printtxt += "TEXT 0," + count + ",\"TSS16.BF2\",0,1,1,\"" + strlist[i] + "\"\r\n";
            }
            int height = Convert.ToInt32(strlist.Count * 4.8);
            P390PrinterEntityCollection Commands1 = new P390PrinterEntityCollection();
            Commands1.Add(new P390PrinterRawProtocolEntity("SIZE 57 mm," + height + " mm\r\n" + "REFERENCE 0,0\r\n" + "SPEED 4.0\r\n" + "DENSITY 8\r\n" + "SET PEEL OFF\r\n" + "SET CUTTER OFF\r\n" + "SET TEAR ON\r\n" + "DIRECTION 0\r\n" + "SHIFT 0\r\n" + "OFFSET 0 mm\r\n" + "CLS\r\n" + printtxt + "PRINT 1,1\r\n"));


            p390Printer1.Print(Commands1, (obj, args) =>
            {
                if (args.isError == true)
                    this.Toast("Error: " + args.error);
                else
                    this.Toast("打印成功");
            });
        }
    }
}